package cn.springcloud.fix.demo.leecode;

public class ReverseList {

    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        node1.setNext(node2);
        node2.setNext(node3);
        ListNode node = reverseList(node1);
        System.out.println(1);
    }

    //反转链表
    public static ListNode reverseList(ListNode head) {
        ListNode pre = null;
        while (null != head) {
            ListNode next = head.getNext();
            head.setNext(pre);
            pre = head;
            head = next;
        }
        return pre;
    }

    //反转链表递归
    public ListNode reverseList2(ListNode head) {
        if (head == null || head.getNext() == null) {
            return head;
        }
        ListNode newHead = reverseList2(head.getNext());
        head.getNext().setNext(head);
        head.setNext(null);
        return newHead;
    }
}
